﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace Yugi_Gui.Animation
{
    public class SwordFlyAnimation
    {
        Image sw_image;
        Storyboard template;
        Point from,to;
        SplineDoubleKeyFrame x1, x2, y1, y2;
        RotateTransform rotate;
        public SwordFlyAnimation(Image sw_image,Storyboard template)
        {
            this.sw_image = sw_image;
            this.template = template;
            Init();
        }

        private void Init()
        {
            DoubleAnimationUsingKeyFrames translate_x = template.Children[0] as DoubleAnimationUsingKeyFrames;
            DoubleAnimationUsingKeyFrames translate_y = template.Children[1] as DoubleAnimationUsingKeyFrames;

            x1 = translate_x.KeyFrames[0] as SplineDoubleKeyFrame;
            x2 = translate_x.KeyFrames[1] as SplineDoubleKeyFrame;

            y1 = translate_y.KeyFrames[0] as SplineDoubleKeyFrame;
            y2 = translate_y.KeyFrames[1] as SplineDoubleKeyFrame;

            TransformGroup group = sw_image.RenderTransform as TransformGroup;
            rotate = group.Children[2] as RotateTransform;

            Storyboard.SetTarget(translate_x, sw_image);
            Storyboard.SetTarget(translate_y, sw_image);
            Storyboard.SetTarget(template.Children[2], sw_image);
        }

        public Point From
        {
            get
            {
                return from;
            }
            set
            {
                from = value;
                Calculate();
            }
        }
        public Point To
        {
            get
            {
                return to;
            }
            set
            {
                to = value;
                Calculate();
            }
        }
        private void Calculate()
        {
            Vector v = new Vector(to.X - from.X, to.Y - from.Y);
            Vector ox = new Vector(to.X - from.X, 0);

            double cos_alpha = ox.X / v.Length;

            double alpha = Math.Acos(cos_alpha);
            alpha *= 180 / Math.PI;
            if (alpha != Double.NaN)
            {
                if (to.Y < from.Y)
                    rotate.Angle = -90 - alpha;
                else
                    rotate.Angle = -90 + alpha;
                x1.Value = from.X;
                x2.Value = to.X;

                y1.Value = from.Y;
                y2.Value = to.Y;
            }
        }
        public void Hide()
        {
            this.sw_image.Visibility = Visibility.Collapsed;
        }
        public void Show()
        {
            this.sw_image.Visibility = Visibility.Visible;
        }
    }
}
